package game;

import behaviors.Position;
import behaviors.Rotation;
import engine.config.Config;
import engine.hw.Gamepad;
import engine.hw.Keys;
import engine.scene.Camera3D;

public class Player
{
    
    private Camera3D camera;
    //<editor-fold defaultstate="collapsed" desc="getCamera">
    public Camera3D getCamera()
    {
        return this.camera;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="getView[Pos/Rot]">
    public Position getViewPos()
    {
        return this.camera.getViewPos();
    }
    
    public Rotation getViewRot()
    {
        return this.camera.getViewRot();
    }
    //</editor-fold>
    
    private ViewMotionKey viewMotionKey;
    //<editor-fold defaultstate="collapsed" desc="getViewMot">
    public ViewMotion getViewMot()
    {
        if (Gamepad.get().isActive())
        {
            return this.viewMotionGamepad;
        }
        else
        {
            return this.viewMotionKey;
        }
    }
    //</editor-fold>
    
    private ViewMotionGamepad viewMotionGamepad;
    
    private ViewRotationMouse viewRotationMouse;
    private ViewRotationGamepad viewRotationGamepad;
    
    public Player(Config configFile)
    {
        this.camera = new Camera3D();
        this.camera.setFOVDeg(configFile.getFloat("FieldOfViewVerticalDegrees"));
        this.camera.setZNear(configFile.getFloat("FrustumNearPlaneZ"));
        this.camera.setZFar(configFile.getFloat("FrustumFarPlaneZ"));
        this.camera.setUpViewMatrices();
        
        this.viewMotionKey = new ViewMotionKey(
            this.camera.getViewPos(), 
            this.camera.getViewRot()
        );
        this.viewMotionKey.getKeyBinding(ViewMotionKey.KEY_FORW).setBoundKey(Keys.W);
        this.viewMotionKey.getKeyBinding(ViewMotionKey.KEY_BACK).setBoundKey(Keys.S);
        this.viewMotionKey.getKeyBinding(ViewMotionKey.KEY_LEFT).setBoundKey(Keys.A);
        this.viewMotionKey.getKeyBinding(ViewMotionKey.KEY_RGHT).setBoundKey(Keys.D);
        this.viewMotionKey.getKeyBinding(ViewMotionKey.KEY_DOWN).setBoundKey(Keys.F);
        this.viewMotionKey.getKeyBinding(ViewMotionKey.KEY_UPWD).setBoundKey(Keys.R);
        
        this.viewRotationMouse = new ViewRotationMouse();
        
        this.viewMotionGamepad = new ViewMotionGamepad(
            this.camera.getViewPos(), 
            this.camera.getViewRot()
        );
        
        this.viewRotationGamepad = new ViewRotationGamepad();
    }
    
    public void update()
    {
        if (Gamepad.get().isActive())
        {
            this.viewRotationGamepad.update(
                this.camera.getViewRot()
            );
            this.viewMotionGamepad.update();
        }
        else
        {
            this.viewRotationMouse.update(
                this.camera.getViewRot()
            );
            this.viewMotionKey.update();
        }
        
        this.camera.update(); // Last so it uses updated values
    }
    
    public void destroy()
    {
        this.camera.destroy();
        this.camera = null;
        this.viewMotionKey.destroy();
        this.viewMotionKey = null;
        this.viewMotionGamepad.destroy();
        this.viewMotionGamepad = null;
        this.viewRotationMouse.destroy();
        this.viewRotationMouse = null;
        this.viewRotationGamepad.destroy();
        this.viewRotationGamepad = null;
    }
    
}
